package org.testis.webautomationmodule.ui.objects;

import org.testis.data.AtofModuleConstants;
import org.testis.utils.StringUtils;
import org.testis.utils.StringVerification;
import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.ui.UIFormFieldDecorator;
import java.util.ArrayList;

/**
 *
 * @author nurhat
 */
public class UILabel extends UIFormFieldDecorator {

    public UILabel(String id) {
        super(id);
    }

    public UILabel(String id,
            String value) {
        super(id,
                value);

    }

    public UILabel(String id,
            String value, boolean isDynamic) {
        super(id,
                value, isDynamic);

    }

    @Override
    protected boolean setBehavior() {
        putLog("Warning you are trying to set a label,label will be verified  with id " + getId(), AtofLogLevel.WARN);
        return verifyBehavior();
    }

    public String normalizeString(String str) {
        str = StringUtils.ClearHTMLTags(str,
                -1);
        str = StringUtils.trimAll(str);

        str = str.replace("\n",
                "");
        str = str.replace("\r",
                "");
        return str;
    }

    @Override
    public boolean getBehavior() {
        String value = "";
        if (!isDynamic()) {
            if (!checkAvailableForVerification()) {
                putLog(" label with id " + getId()
                        + " was not verified.It is not available for verification",
                        AtofLogLevel.ERROR);
                setValue("");
                return false;
            }
           
        }
        String[] lblIdList = null;
        if (!isDynamic()) {
            lblIdList = new String[1];
            lblIdList[0] = getId();
        } else {
            lblIdList = getDynamicIdList();
        }
        for (int i = 0; i < lblIdList.length; i++) {
            String output = getWebAutomationImpl().getText(lblIdList[i]);
            if (value.isEmpty()) {
                value = output;
            } else {
                value = value + "-" + output;
            }
        }
        setValue(value);
        putLog(" Value of label with id : " + getId()
                + "  is:" + getValue(),
                AtofLogLevel.INFO);

        return true;

    }

    @Override
    public boolean verifyBehavior() {
        if (!isDynamic()) {
            if (!checkAvailableForVerification()) {
                putLog(" label with id " + getId()
                        + " was not verified.It is not available for verification",
                        AtofLogLevel.ERROR);
                setValue("");
                return false;
            }
        }
        String[] lblIdList = null;
        if (!isDynamic()) {
            lblIdList = new String[1];
            lblIdList[0] = getId();
        } else {
            lblIdList = getDynamicIdList();
        }
        StringBuilder comparedWith = new StringBuilder();
        for (int i = 0; i < lblIdList.length; i++) {
            String output = getWebAutomationImpl().getText(lblIdList[i]);
            m_value = normalizeString(m_value);
            output = normalizeString(output);
            String valueToCompare = StringUtils.removeSpaces(getValue());
            if (i == 0) {
                comparedWith.append(output);
            } else {
                comparedWith.append("," + output);
            }
            boolean result =
                    StringVerification.verifyText(valueToCompare,
                    StringUtils.removeSpaces(
                    output));
            if (result) {
                putLog(" Label with " + getId()
                        + " id was  verified... curValue:"
                        + output
                        + " compared with:"
                        + getValue());
                setValue(output);
                return true;
            }
        }
        putLog(" Label with " + getId()
                + " id was  not verified with value:" + getValue() + " compared with following values:" + comparedWith.toString(),
                AtofLogLevel.ERROR);

        return false;
    }

    private String[] getDynamicIdList() {
        if (isElementPresent()) {
            String[] arr = new String[1];
            arr[0] = getId();
            return arr;
        }
        int lastIndex = getId().length() - 1;
        String idToCompare = getId().substring(0, lastIndex - 1);
        String idList = getWebAutomationImpl().getEval(
                "function findDynamicLabels(objId){ "
                + "  tags = selenium.browserbot.getDocument().getElementsByTagName('*'); "
                + "  var ids=''; "
                + " for (var i=0; i< tags.length; i++) {"
                + "    if(tags[i].id){"
                + "      if (tags[i].id.startsWith(objId))"
                + "       { if(ids==''){ ids=tags[i].id; }"
                + "         else{ids=ids+'" + AtofModuleConstants.PARAM_SEP_STR + "'+ tags[i].id;} "
                + "       }"
                + "    }"
                + "  } "
                + "  return ids;"
                + "}; "
                + "eval('findDynamicLabels(\"" + idToCompare + "\")');");
        if (!idList.isEmpty()) {
            return idList.split(AtofModuleConstants.PARAM_SEP_SPLIT_STR);

        }
        return null;
    }
}
